<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Survey extends TMT_Controller {
	
 	function __construct() {
        parent::__construct();

    }
	
    public function index()
    {
    	$this->set_layout_view('');
    	$this->load->helper('form');
    	
    	if($this->input->get('survey_id') === FALSE)
    	{
    		redirect('/');
    	}
    	
    	if($this->input->get('success'))
    		$this->view_data['success'] = TRUE;
    	
    	$survey = Model\Survey::find($this->input->get('survey_id'));
    	
    	if($survey)
    	{
	    	$employee = $survey->employee();
	    	
	    	
	    	if($survey)
	    		$trainings = $survey->traininggroup()->training();
	    	
	    	
	    	$this->view_data['trainings'] = array();
	    	foreach($trainings as $training)
	    	{
	    		if(!Model\Confirmation::where(array('employee_id' => $employee->id, 'training_id' => $training->id))->all(FALSE))
	    			$this->view_data['trainings'][] = $training;
	    	}
	    	
	    	if(count($this->view_data['trainings']) !== 0)
	    		$this->view_data['survey'] = $survey;
    	}
    	return;
    }
    
    public function submit_survey()
    {
    	$this->set_layout_view('');
    	$this->load->helper('form');
    	 
    	if($this->input->post('survey_id') === FALSE)
    	{
    		redirect('/');
    	}
    	 
    	$survey = Model\Survey::find($this->input->post('survey_id'));
    	 
    	if(!$survey)
    	{
    		echo 'Die Umfrage ist nicht oder nicht mehr gültig!';
    		return;
    	}
    	 
    	//$this->view_data['trainings'] = $survey->traininggroup()->training();
    	
    	$employee = $survey->employee(); 
    	
    	foreach($this->input->post('training') as $key => $value)
    	{
    		if($value == '')
    			continue;
    		
    		
    		$confirmation = new Model\Confirmation();
    		$confirmation->employee_id = $employee->id;
    		$confirmation->training_id = $key;
    		$confirmation->confirmed = $value;
    		
    		$confirmation->save();
    		
    	}
    	
    	redirect('survey/?success=true&survey_id='.$this->input->post('survey_id'));
    }
    
    
    
	
}